<?php
class Home_Service_Common extends My_Service{
	#获取默认apps路径
	function getDefaultDirectory(){
		$defaultAppName = Zend_Controller_Front::getInstance()->getDefaultModule();
		return Zend_Controller_Front::getInstance()->getModuleDirectory($defaultAppName);
	}
	
	#获取网站识别码
	function getSiteName(){
		$config = My_Config::getInstance()->getOptions();
		return $config['appname'];
	}
	
	#检查验证码
	function checkCaptcha($captcha){
		My_Tool::importOpen("php_verify/verify.php");
		$session = new My_Session_Namespace(YL_Security_Secoder::$seKey);
		$code = $session->code;
		if(strtolower($code) == strtolower($captcha)) return true;
		return false;
	}
	
	#获取app路径
	function getAppPath($appName=''){
		if($appName){
			$appName = strtolower($appName);
			return Zend_Controller_Front::getInstance()->getModuleDirectory($appName);
		}else{
			return Zend_Controller_Front::getInstance()->getModuleDirectory();
		}	
	}
	
	#清空app文件
	function clearAppFile($appName){
		$appName = strtolower($appName);
		$path = APPLICATION_PATH . DS.$appName;
		My_Tool_File::deldir($path);
		$resPath = ROOT_DIR."/res/asset/" . $appName;
		My_Tool_File::deldir($resPath);
		$libPath = ROOT_LIB."/".ucfirst($appName);
		My_Tool_File::deldir($libPath);
		return true;
	}
	
	#卸载
	function uninstall($appName){
		if(!$appName) return false;
		set_time_limit(0);
 		$appPath = $this->getAppPath($appName);
		$sqlPath = $appPath."/data/sql/uninstall.sql";
		if(is_file($sqlPath)){
			My_Tool::importOpen("Install.class.php");
			$sqlArr = FInstall::mysql($sqlPath);
			if($sqlArr){
				foreach ($sqlArr as $sv){
					Home::dao()->get()->exec($sv, array(), 0);
				}
			}
		}
		//删除文件
		$this->clearAppFile($appName);
		return true;
	}
	
	#安装
	function install($appName){
		if(!$appName) return false;
		set_time_limit(0);
 		$appPath = APPLICATION_PATH . DS. strtolower($appName);
		$sqlPath = $appPath."/data/sql/install.sql";
		if(is_file($sqlPath)){
			My_Tool::importOpen("Install.class.php");
			$sqlArr = FInstall::mysql($sqlPath);
			if($sqlArr){
				foreach ($sqlArr as $sv){
					Home::dao()->get()->exec($sv, array(), 0);
				}
			}
 		}
		return true;
	}
	
}